import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:juju/conf/IMConf.dart';

class AddFriend extends StatefulWidget {
  const AddFriend({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _AddFriendState();
}

class _AddFriendState extends State<AddFriend> {
  final TextEditingController _usernameTextController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("添加好友"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.fromLTRB(30.0, 10.0, 30.0, 10.0),
              child: TextField(
                controller: _usernameTextController,
                decoration: const InputDecoration(
                  border: OutlineInputBorder(),
                  labelText: "账号",
                ),
              ),
            ),
            Padding(
              // padding: EdgeInsets.all(10.0),
              padding: const EdgeInsets.fromLTRB(30.0, 10.0, 30.0, 10.0),
              child: Row(
                children: <Widget>[
                  Expanded(
                    child: FloatingActionButton(
                      foregroundColor: Colors.blue[300],
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(50.0),
                      ),
                      onPressed: _addFriend,
                      child: const Text("添加"),
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  void _addFriend() async {
    await IMConf.getInstance()
        .getJmessage()
        .sendInvitationRequest(
            username: _usernameTextController.text, reason: '请求添加好友')
        .then(
      (value) => print("===================== 已发送加好友请求 ======================"),
      onError: (e) {
        if (e is PlatformException) {
          PlatformException exception = e;
          print(
              "=============== 添加失败 ===============\n${exception.code}\n${exception.details}\n${exception.message}\n===========================");
        } else {
          print(e.toString());
        }
      },
    );
  }
}
